package stream;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author deity
 * @version 1.0
 * @date 2020/11/8 14:43
 *
 */
public class StreamApp {


    public static void main(String[] args) {

        System.out.println("可用处理器数量: "+Runtime.getRuntime().availableProcessors());

        // 1-添加测试数据:存储多个账户的列表
        List<String> accounts = new ArrayList<>();
        accounts.add("tom");
        accounts.add("deity");
        // 1-1-业务要求:长度 >=5 才是有效账户

        //(1)传统for循环
        for (String account : accounts) {
            if (account.length() >= 5) {
                System.out.println("有效账户:" + account);
            }
        }
        //(2)迭代器
        Iterator<String> it = accounts.iterator();
        while (it.hasNext()) {
            String account = it.next();
            if (account.length() >= 5) {
                System.out.println("it有效账户:" + account);
            }
        }
        //(3) Stream结合Lambda表达式，完成业务处理
        List validAccounts = accounts.stream().filter(s -> s.length() >= 5).collect(Collectors.toList());
        System.out.println(validAccounts);


        //(4) flatMap() 会把流切分，重新组装流
        List<String> nameList = Arrays.asList("AA","BB");
        List<List<String>> testList = Arrays.asList(accounts,nameList);
        System.out.println("--------------map()");
        testList.stream().map(x->x+"-").forEach(System.out::println);

        System.out.println("--------------flatMap()");
        testList.stream().flatMap(x->x.stream()).forEach(System.out::println);


    }

}
